app
		.service(
				"supplierService",
				function($http, ajaxRequest) {
					return {
						getListData : function(obj) {
							return ajaxRequest.request($http.get(
									"/api/supplier/getListData", {
										params : obj
									}));
						},
						updateIsUse : function(obj) {
							return ajaxRequest.request($http.get(
									"/api/supplier/updateIsUse", {
										params : obj
									}));
						},
						getListBysupplierId : function(obj) {
							return ajaxRequest.request($http.get(
									"/api/supplierGoods/getListData", {
										params : obj
									}));
						},
						getGoodsList : function(obj) {
							return ajaxRequest.request($http.get(
									"/api/supplierGoods/getGoodsList", {
										params : obj
									}));
						},
						getGoodsPageList : function(obj) {
							return ajaxRequest.request($http.get(
									"/api/supplierGoods/getGoodsPageList", {
										params : obj
									}));
						},
						getSupplierState : function() {
							return ajaxRequest.request($http
									.get("/api/supplier/supplierState"));
						},
						getSettlementTypes : function() {
							return ajaxRequest.request($http
									.get("/api/supplier/settlementTypes"));
						},
						getSupplierTypes : function() {
							return ajaxRequest.request($http
									.get("/api/supplier/supplierTypes"));
						},
						getInvoiceTypes : function() {
							return ajaxRequest.request($http
									.get("/api/supplier/invoiceTypes"));
						},
						getDetaillistData : function(orderId) {
							return ajaxRequest.request($http
									.get("/api/supplier/detail?orderId="
											+ orderId));
						},
						saveDetail : function(obj) {
							return ajaxRequest.request($http.post(
									"/api/supplier/save", obj));
						},
						deleteGoods : function(obj) {
							return ajaxRequest.request($http.post(
									"/api/supplierGoods/delete", obj));
						},
						addGoods :function(obj) {
							return ajaxRequest.request($http.post(
									"/api/supplierGoods/addGoods", obj));
						}
			
					}
				})
		.controller(
				"supplierIndexController",
				function($scope, supplierService) {
					$scope.isUse = 1;
					$scope.searchData = {
						supplierCode : $scope.supplierCode,
						supplierName : $scope.supplierName,
						settmentType : $scope.settmentType,
						isExamine : $scope.isExamine,
						isUse : $scope.isUse,
						pageIndex : 1,
						pageNum : 10
					};
					$scope.Export =function() {
						if($scope.settmentType==undefined)
							$scope.settmentType=-1;
						if($scope.supplierName==undefined)
							$scope.supplierName="";
						if($scope.supplierCode==undefined)
							$scope.supplierCode="";
						window.location.href ="/api/supplier/exportCsv?isUse="+$scope.isUse+"&supplierCode="
						+$scope.supplierCode+"&supplierName="+$scope.supplierName+"&settmentType="+$scope.settmentType;
					};
					
					$scope.Reset = function() {

						$scope.supplierCode = ""
						$scope.supplierName = ""
						$scope.settmentType = -1;
						$scope.isExamine = -1;

						$scope.searchData.pageIndex = 1;
						$scope.searchData.pageNum = 10;
					};

					var requestListData = function() {
						$scope.searchData.supplierCode = $scope.supplierCode;
						$scope.searchData.supplierName = $scope.supplierName;
						$scope.searchData.settmentType = $scope.settmentType;
						$scope.searchData.isExamine = $scope.isExamine;
						$scope.searchData.isUse = $scope.isUse;
						supplierService.getListData($scope.searchData).then(
								function(data) {
									var re = data.result;
									$scope.supplierList = re.entityList;
									$scope.CurrentPage = re.curPageIndex;
									$scope.RecordCount = re.recordCount;
									$scope.ShowPages = re.pageCountList;
									$scope.PageCount = re.pageCount;
								}, function(data) {
									Kalert(data.msg);
								});
					}

					var requestOrderTypes = function() {
						supplierService.getSettlementTypes().then(
								function(data) {
									$scope.orderTypes = data;
								}, function(data) {
									Kalert(data.msg);
								});
					}
					var requestSupplierState = function() {
						supplierService.getSupplierState().then(function(data) {
							$scope.supplierstates = data;
						}, function(data) {
							Kalert(data.msg);
						});
					}
					requestSupplierState();
					requestOrderTypes();
					requestListData();

					$scope.GotoPage = function(pageindex) {
						if (pageindex <= 0)
							return;
						if (pageindex > $scope.PageCount)
							return;
						$scope.searchData.pageIndex = pageindex;
						requestListData($scope.searchData);
					};
					
	
					$scope.Search = function() {
						$scope.searchData.pageIndex = 1;
						requestListData();
					}
					
					$scope.use = function(id,isUse) {
						$scope.upUse=
							{
								id:id,
								isUse:isUse
							}
						supplierService.updateIsUse($scope.upUse).then(
								function(data) {
									Kalert("操作成功","系统提示",requestListData);
									
								}, function(data) {
									Kalert(data.msg);
								});
						
					}
					var RemoveCss = function() {
						$("#Using").removeClass("active");
						$("#NoUsed").removeClass("active");
						$("#All").removeClass("active");
					};
					
					$scope.TabClick = function(id, isUse) {
						RemoveCss();
						$("#" + id).addClass("active");
						$scope.isUse = isUse;
						
						$scope.searchData.pageIndex = 1;
						requestListData();
					};
					
					$scope.ShowDetail = function(supplier) {
						window.location.href = "/web/supplier/detail?id="
								+ supplier.id + "&supplierCode="
								+ supplier.supplierCode + "&supplierName="
								+ supplier.supplierName + "&settmentType="
								+ supplier.settmentType + "&deposit="
								+ supplier.deposit + "&goodsClass="
								+ supplier.goodsClass + "&beneficiaryName="
								+ supplier.beneficiaryName + "&openAccount="
								+ supplier.openAccount + "&bankAccount="
								+ supplier.bankAccount + "&contractStartTime="
								+ supplier.contractStartTime
								+ "&contractEndTime="
								+ supplier.contractEndTime + "&contractPerson="
								+ supplier.contractPerson + "&contractMobile="
								+ supplier.contractMobile + "&isUseStr="
								+ supplier.isUseStr + "&isExamineStr="
								+ supplier.isExamineStr + "&managePerson="
								+ supplier.managePerson + "&remark="
								+ supplier.remark + "&supplierType="
								+ supplier.supplierType + "&invoiceType="
								+ supplier.invoiceType + "&supplierShortCode="
								+ supplier.supplierShortCode;
					}

					$scope.Detail = function(orderId) {
						window.location.href = "/web/supplier/detail?orderId="
								+ orderId;
					}

					// 编辑
					$scope.Edit = function(orderId) {
						window.location.href = "/web/supplier/edit?orderId="
								+ orderId;
					}

					$scope.goodsMaintain = function(orderId,name) {
						window.location.href = "/web/supplierGoods/index?supplierId="
								+ orderId+"&supplierName="
								+ name;
					}

					$scope.Add = function() {
						window.location.href = "/web/supplier/edit?orderId=" + 0;
					}
					

				})
		.controller(
				"supplierDetailController",
				function($scope, supplierService) {

					var orderId = GetQueryString("orderId");
					$scope.searchData = {
						orderId : orderId
					};
					var requestDetail = function() {
						supplierService.getDetaillistData(orderId).then(
								function(data) {
									$scope.DetaillistData = data.result;
								}, function(data) {
									jAlert(data.msg);
								});
					}
					requestDetail();
					$scope.back = function() {
						window.location.href = "/web/supplier/index";
					}
				})
		.controller(
				"supplierEditController",
				function($scope, supplierService) {

					var orderId = GetQueryString("orderId");

					if (orderId == "0") {
						$scope.DetaillistData = {};
						$scope.isDisabled = false;
						$scope.DetaillistData.id = 0;
						$scope.DetaillistData.isUse = 1;
						$scope.DetaillistData.isExamine = 1;
						$scope.DetaillistData.isUseStr = "";
						$scope.DetaillistData.isExamineStr = "";
						$scope.DetaillistData.settmentTypeStr = "";
						$scope.DetaillistData.invoiceTypeStr = "";
						$scope.DetaillistData.supplierTypeStr = "";

					} else {
						supplierService.getDetaillistData(orderId).then(
								function(data) {
									$scope.DetaillistData = data.result;
								}, function(data) {
									Kalert(data.msg);
								});
						$scope.isDisabled = true;
					}

					var requestOrderTypes = function() {
						supplierService.getSettlementTypes().then(
								function(data) {
									$scope.orderTypes = data;
								}, function(data) {
									Kalert(data.msg);
								});
					}

					var requestInvoiceTypes = function() {
						supplierService.getInvoiceTypes().then(function(data) {
							$scope.InvoiceTypes = data;
						}, function(data) {
							Kalert(data.msg);
						});
					}
					var requestSupplierTypes = function() {
						supplierService.getSupplierTypes().then(function(data) {
							$scope.supplierTypes = data;
						}, function(data) {
							Kalert(data.msg);
						});
					}
					requestSupplierTypes();
					requestInvoiceTypes();
					requestOrderTypes();

					$scope.submit = function() {
						supplierService.saveDetail($scope.DetaillistData).then(
								function(data) {
									Kalert("保存成功","系统提示",$scope.back);
								}, function(data) {
									Kalert(data.msg);
								});
					}

					$scope.dell = function(rowindex) {
						if ($scope.supplierorderList.length > 1) {
							$scope.supplierorderList.splice(rowindex, 1);
						}
					}
					$scope.back = function() {
						window.location.href = "/web/supplier/index";
					}
					
					$scope.changeNum = function(){
	
					if (isNaN($scope.DetaillistData.deposit)) {
						$scope.DetaillistData.deposit = "";
						return false;
					}
					if ($scope.DetaillistData.deposit.indexOf(".") == 0) {
						$scope.DetaillistData.deposit = $scope.DetaillistData.deposit.replace(".", "");
					}
					if ($scope.DetaillistData.deposit.length > 10) {
						$scope.DetaillistData.deposit = "";
					}
//					if($scope.DetaillistData[index].inQuantity2)
//					{
//						if(parseInt($scope.DetaillistData.deposit)+parseInt($scope.DetaillistData[index].inQuantity2) > $scope.DetaillistData[index].arrivalQuantity){
//							$scope.DetaillistData.deposit = "";
//						}
//					}
//					else
//					{
//						if(parseInt($scope.DetaillistData.deposit) > $scope.DetaillistData[index].arrivalQuantity){
//							$scope.DetaillistData.deposit = "";
//						}
//					}
					var firstchar = $scope.DetaillistData.deposit.substr(0,1);
					if(firstchar == "0" && $scope.DetaillistData.deposit.length>1)
					{
						if($scope.DetaillistData.deposit.indexOf(".") !=1)
							$scope.DetaillistData.deposit = "";
					}
					
				}
				})
		.controller(
				"supplierGoodsController",
				function($scope, supplierService) {
					var mid = GetQueryString("supplierId");
					var supplierName = GetQueryString("supplierName");
					$scope.supplierName =decodeURI(supplierName);
					$scope.searchData = {
						taosku : $scope.taosku,
						bn : $scope.bn,
						goodsName : $scope.goodsName,
						supplierId : mid
					};


					var requestListData = function() {
						$scope.searchData.taosku = $scope.taosku;
						$scope.searchData.bn = $scope.bn;
						$scope.searchData.goodsName = $scope.goodsName;
						$scope.searchData.supplierId = mid;
						supplierService.getListBysupplierId($scope.searchData)
								.then(function(data) {

									$scope.supplierGoodsList = data.result;
									if($scope.supplierGoodsList.length>=8)
										$('#scrollTh')[0].style.width='0.5%';
									else
										{
											$('#scrollTh')[0].hidden="hidden";
											$('#scrollTh')[0].style.width='0%';
										}
								}, function(data) {
									Kalert(data.msg);
								});
					}

					requestListData();
					$scope.supplierAllIsSelected=false;
					$scope.Search = function() {
						$scope.supplierAllIsSelected=false;
						requestListData();
					}
					
					$scope.Reset= function() {
						$scope.taosku="";
						$scope.bn="";
						$scope.goodsName="";
					}
					
					$scope.sel = function(index) {
						for (var i = 0; i < $scope.supplierGoodsList.length; i++) {
							if (i == index) {
								if ($scope.supplierGoodsList[i].IsSelected == false) {
									$scope.supplierGoodsList[i].IsSelected = false;
								} else {
									$scope.supplierGoodsList[i].IsSelected = true;
								}
							}
						}
					}

					$scope.selectAll = function() {
						for (var i = 0; i < $scope.supplierGoodsList.length; i++) {
							$scope.supplierGoodsList[i].IsSelected = $scope.supplierAllIsSelected;
						}
					}

					$scope.dbc = function(index) {
						$scope.supplierGoodsList[index].IsSelected = true;

					}

					var deleteGoods = function() {
						$scope.deleteList = [];
						angular.forEach($scope.supplierGoodsList, function(sg) {
							var si = sg.IsSelected;
							if (si == true && si != undefined) {
								delete sg.IsSelected;
								$scope.deleteList.push(sg);
							}
						});
						if ($scope.deleteList.length == 0) {
							Kalert("请选择一条数据!", "系统提示");
							return;
						} else {
							supplierService.deleteGoods($scope.deleteList)
									.then(function(data) {

										Kalert("删除成功", "系统提示",requestListData);
									}, function(data) {
										Kalert(data.msg);
									});

						}
					}
//					var setWidth =function()
//					{
//						var scrollbarWidth = $('#main')[0].offsetWidth - $('#main')[0].scrollWidth;
//						if(scrollbarWidth<=5)
//							$('#scrollTh')[0].style.width='0.5%';
//						else
//							$('#scrollTh')[0].style.width='0.5%';
//					}
//					
//					setWidth();
//					window.onresize = function () {
//						setWidth();
//				    }
					
					$scope.returnSupplier=function(){
						window.location.href = "/web/supplier/index";
					}
					$scope.Delete = function() {
						deleteGoods();
					}
					$scope.Add = function() {
						window.location.href = "/web/supplierGoods/selectGoods?supplierId="+mid+"&supplierName="+$scope.supplierName;
					}
				}).filter('cut', function () {
					  return function (value, wordwise, max, tail) {
						    if (!value) return '';

						    max = parseInt(max, 10);
						    if (!max) return value;
						    if (value.length <= max) return value;

						    value = value.substr(0, max);
						    if (wordwise) {
						      var lastspace = value.lastIndexOf(' ');
						      if (lastspace != -1) {
						        value = value.substr(0, lastspace);
						      }
						    }

						    return value + (tail || ' …');
					};
				}).controller("selectGoodsController", function($scope, supplierService) {
					
					var mid = GetQueryString("supplierId");
					var supplierName = GetQueryString("supplierName");
					$scope.supplierName = decodeURI(supplierName);
					$scope.searchData = {
						sku : $scope.sku,
						barcode : $scope.barcode,
						goodsName : $scope.goodsName,
						pageIndex : 1,
						pageNum : 10
					};

					var requestListData = function() {
						$scope.searchData.taosku = $scope.taosku;
						$scope.searchData.bn = $scope.bn;
						$scope.searchData.goodsName = $scope.goodsName;
						$scope.searchData.pageIndex = 1;
						$scope.currentPage =1;
						$scope.pages=2;
						supplierService.getGoodsPageList($scope.searchData).then(function(data) {							
							$scope.goodsList = data.result;
						}, function(data) {
				           Kalert(data.msg);
						});
					}

					requestListData();
					$scope.goodsAllIsSelected=false;
					$scope.Search = function() {
						$scope.goodsAllIsSelected=false;
						requestListData();
					}
						
					$scope.GotoPage = function(pageindex) {
						if (pageindex <= 0)
							return;
						if (pageindex > $scope.PageCount)
							return;
						$scope.searchData.pageIndex = pageindex;
						requestListData($scope.searchData);
					};
					
					$scope.Reset= function() {
						$scope.taosku="";
						$scope.bn="";
						$scope.goodsName="";
					}
					
					$scope.sel = function(index) {
						for (var i = 0; i < $scope.goodsList.length; i++) {
							if (i == index) {
								if($scope.goodsList[i].IsSelected == false){
									$scope.goodsList[i].IsSelected = false;
								}else{
									$scope.goodsList[i].IsSelected = true;
								}
							} 
						}
					}
					
					$scope.dbc = function(index){
						$scope.goodsList[index].IsSelected = true;
					}
					
					$scope.selectAll = function() {
						for (var i = 0; i < $scope.goodsList.length; i++) {
							$scope.goodsList[i].IsSelected = $scope.goodsAllIsSelected;
						}
					}
					
					$scope.confirm = function() {
						confirm();
					}
					
					var confirm = function(){
						 $scope.addGoodsList = [];
						angular.forEach($scope.goodsList, function(go) {
							var si = go.IsSelected;
							if (si == true && si != undefined) {
								delete go.IsSelected;
								go.supplierId = mid;
								$scope.addGoodsList.push(go);
							}
						});
						if ($scope.addGoodsList.length == 0) {
							Kalert("请选择一条数据!", "系统提示");
							return;
						} else {
							supplierService.addGoods($scope.addGoodsList)
							.then(function(data) {

								Kalert("提交成功","系统提示",returnSupplierGoods);

							}, function(data) {
								Kalert(data.msg);
							});
							
						
						} 
					}
					
					$scope.cancel = function(){
						returnSupplierGoods();
					};


				    var returnSupplierGoods = function(){
					window.location.href = "/web/supplierGoods/index?supplierId="
						+ mid+"&supplierName="+$scope.supplierName;
					 }
				
				$scope.loadMore = function() { 
					$scope.pages = Math.ceil($scope.goodsList.length/$scope.searchData.pageNum); 
				    if ($scope.currentPage <= $scope.pages) { 
				      $scope.currentPage++; 
				      if ($scope.busy) { 
				        return false; 
				      } 
				      $scope.busy = true; 
				      $scope.searchData.pageIndex= $scope.currentPage;
						supplierService.getGoodsPageList($scope.searchData).then(function(data) {							
							angular.forEach(data.result,function(go){
								$scope.goodsList.push(go);
							});
							
							$scope.busy = false;
						}, function(data) {
							$scope.busy = false;
				           Kalert(data.msg);
						});
				     				        
				        
				        
				    } 
				  };
				});
function GetQueryString(sProp) {
	var re = new RegExp("[&,?]" + sProp + "=([^//&]*)", "i");
	var a = re.exec(window.location.search);
	if (a == null) {
		return "";
	} else {
		return a[1];
	}
}
app.directive('whenScrolled', function() { 
	  return function(scope, elm, attr) { 
	    var raw = elm[0]; 
	    elm.bind('scroll', function() { 
	      if (raw.scrollTop+raw.offsetHeight >= raw.scrollHeight) { 
	        scope.$apply(attr.whenScrolled); 
	      } 
	    }); 
	  }; 
	});

